package JdbcTest.Jdbc;

import JdbcTest.JdbcUtil.JdbcUtil;
import JdbcTest.pojo.Student;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JdbcTest5 {
    public static void main(String[] args) {
        //注册驱动及获取数据库连接对象
        Connection conn = JdbcUtil.MyConnection();
        //获取数据库操作对象
        PreparedStatement pst = null;
        //查询表中全部数据
        ResultSet rs = JdbcUtil.select(conn);
        try {
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                boolean sex = rs.getBoolean("sex");
                String eMail = rs.getString("email");
                Student student = new Student(id,name,sex,eMail);
                System.out.println(student);
            }
           //根据id修改表中数据
           String sql = "update t_student set eMail = ? where id = ?";
            pst = conn.prepareStatement(sql);
            pst.setString(1,"ww@qq.com");
            pst.setInt(2,3);
            int num = pst.executeUpdate();
            System.out.println(num==1?"修改成功":"修改失败");

        rs = JdbcUtil.select(conn);
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                boolean sex = rs.getBoolean("sex");
                String eMail = rs.getString("email");
                Student student = new Student(id,name,sex,eMail);
                System.out.println(student);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.MyClose(conn,pst,rs);
        }
    }
}
